var app = getApp()
Page({

  data: {
    userName: '',
    userPsw: '',
    countTime: 60,
    userTempInfo: '',
    userTempCode: '',
 
  },
  onLoad: function(options) {
    var that = this
    wx.showLoading({
      title: '请稍候',
    })
    app.globalData.isLoginPage=true
  },
  countTime: function() {
    var that = this
    if (this.data.countTime === 60) { // 解决第一次点击时的延时
      this.setData({
        countTime: that.data.countTime - 1
      })
      that.countTime()
    } else {
      setTimeout(function() {
        if (that.data.countTime === 0) {
          that.setData({
            countTime: 60
          })

        } else {
          that.setData({
            countTime: that.data.countTime - 1
          })
          that.countTime()
        }

      }, 1000);
    }


  },

  sendCode: function() {
    if (this.data.userName == '' || this.data.userName.length != 11) {
      wx.showModal({
        title: '提示',
        content: '请正确输入手机号',
        showCancel: false
      })
    } else if (this.data.countTime != 60) {
      wx.showModal({
        title: '提示',
        content: '请稍后',
        showCancel: false
      })
    } else {
      this.countTime()
      var that = this
      wx.request({
        url: 'https://xiaochengxu.fincoder.com/index.php/Home/GetCode/loginCode/',
        data: {
          mobile: that.data.userName,

        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          if (res.data.status === 1) {

          } else {
            wx.showModal({
              title: '提示',
              content: '获取失败' + res.data.Message,
              showCancel: false
            })
          }
        },
        fail: function(e) {

        }

      })
    }

  },
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({

      url: '/pages/index/index',

    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })
    wx.hideLoading()
  },
  onShow: function() {

  },

  toIntro: function() {
    wx.navigateTo({
      url: '../index/page/verifyInfo/verifyInfo'
    })
  },
  userNameInput: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  userPswInput: function(e) {

    this.setData({
      userPsw: e.detail.value
    })
  },
  onGotUserInfo: function(e) {
    console.log('akakaak')
    this.getUserInfo1()

  },
  getUserInfo1: function(e) {

    if (this.data.userName.length == 0 || this.data.userPsw.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请输入完整信息',
        showCancel: false
      })
    } else {
      console.log('rr')
      wx.showLoading({
        title: '请稍候',
      })
      var that = this
      wx.login({
        success: function(res) {
          var code = res.code;
         
          wx.getUserInfo({
            success: function(ress) {
           
              that.setData({
                userTempInfo: ress,
                userTempCode: code
              })
              that.login()
             
            },
            fail: function(ee) {
              wx.hideLoading()
              console.log(ee)
              wx.showModal({
                title: '授权提示',
                content: '小程序需要您的微信授权才能使用哦~ 错过授权页面的处理方法：删除小程序->重新搜索进入->点击授权按钮'
              })
            }
          })

        },
        fail: function() {
          wx.hideLoading()
        },
        fail: function() {
          wx.hideLoading()
        }
      })

    }
  },

  login: function(rd) {

    var that = this
    if (that.data.userName.length == 0 || that.data.userPsw.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请填写完整信息',
        showCancel: false
      })
    } else {


      wx.request({
        url: 'https://xiaochengxu.fincoder.com/index.php/Home/LoginXcx/index',
        data: {
          mobile: that.data.userName,
          captcha: that.data.userPsw,
          code: that.data.userTempCode,
          rawData: that.data.userTempInfo.rawData,
          encryptedData: that.data.userTempInfo.encryptedData,
          signature: that.data.userTempInfo.signature,
          iv: that.data.userTempInfo.iv,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
      
          wx.hideLoading()
          if (res.data.status === 1) {

            app.globalData.member = res.data.member
            app.globalData.myId = res.data.member.openId
            
            wx.setStorageSync('myId', res.data.member.openId)
         
              wx.navigateBack({ changed: true })
            
          } else {
            wx.showModal({
              title: '提示',
              content: '绑定失败' + res.data.msg,
              showCancel: false
            })
          }
        },
        fail: function(e) {
         
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '绑定失败',
            showCancel: false
          })
        }

      })
    }

  },
  goRegister: function() {
    wx.navigateTo({
      url: '../index/page/verifyInfo/verifyInfo',

    })
  }

})